package main

import "fmt"

var ok bool

func main() {
	sum := 0
	for i := 1000; i < 10000; i++ {
		ok = false
		factors := DecomposingFactor(i)
		getSum(0, 1, i, factors)
		if ok {
			sum += i
		}
	}
	fmt.Println(sum)
}

// DecomposingFactor 分解质因数
func DecomposingFactor(n int) []int {
	factors := make([]int, 0)
	for i := 2; i <= n; i++ {
		if n%i == 0 {
			n /= i
			factors = append(factors, i)
			i--
		}
	}
	return factors
}

// isPandigital 判断结果
func isPandigital(multiplicand, multiplier, product int) bool {
	digits := make([]int, 0)
	for multiplicand > 0 {
		digits = append(digits, multiplicand%10)
		multiplicand /= 10
	}
	for multiplier > 0 {
		digits = append(digits, multiplier%10)
		multiplier /= 10
	}
	for product > 0 {
		digits = append(digits, product%10)
		product /= 10
	}
	if len(digits) != 9 {
		return false
	}
	m := make(map[int]bool)
	for _, digit := range digits {
		m[digit] = true
	}
	if len(m) != 9 {
		return false
	}
	if m[0] {
		return false
	}
	return true
}

func getSum(n, multiplicand, product int, factors []int) {
	if n >= len(factors) {
		if isPandigital(multiplicand, product/multiplicand, product) {
			ok = true
		}
	} else {
		getSum(n+1, multiplicand, product, factors)
		getSum(n+1, multiplicand*factors[n], product, factors)
	}

}
